package com.whisperly.echotalk.starryb.utils;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.whisperly.echotalk.bllmodel.ChatWithUser;
import com.whisperly.echotalk.bllmodel.UserWithChat;

import java.util.ArrayList;
import java.util.List;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "BlinkChat.db";

    private static final String TABLE_NAME = "chat_history";
    private static final String COLUMN_USER_ID = "user_id";
    private static final String COLUMN_MESSAGE = "message";
    private static final String COLUMN_STATE = "state";
    private static final String  CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COLUMN_USER_ID + " TEXT, "
            + COLUMN_MESSAGE + " TEXT, "
            + COLUMN_STATE + " INTEGER)";

    public static final String TABLE_NAME_USER = "table_user";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_PASSWORD = "password";
    private static final String COLUMN_PHONE = "phone";
    private static final String COLUMN_EMAIL = "email";
    private static final String COLUMN_REL_NAME = "relName";
    private static final String COLUMN_GENDER = "gender";
    private static final String COLUMN_LOGIN_TIME = "login_time";
    private static final String COLUMN_LOGIN_id = "login_id";
    private static final String CREATE_TABLE_USER = "CREATE TABLE " + TABLE_NAME_USER + "(id INTEGER PRIMARY KEY AUTOINCREMENT,"
            + COLUMN_NAME + " TEXT, "
            + COLUMN_PASSWORD + " TEXT, "
            + COLUMN_EMAIL + " TEXT, "
            + COLUMN_PHONE + " TEXT, "
            + COLUMN_LOGIN_TIME + " TEXT, "
            + COLUMN_GENDER + " INTEGER, "
            + COLUMN_REL_NAME + " TEXT, "
            + COLUMN_LOGIN_id + " TEXT)";

    private static final String TABLE_NAME_FEED = "CREATE TABLE feedback (id INTEGER PRIMARY KEY AUTOINCREMENT,feedbackType TEXT,feedbackContent TEXT)";

    private static final String firstSql = "INSERT INTO table_user(name,password,email,phone,gender,relName,login_id) VALUES('testUser','2e36e8c73c392f59fbb1e4001985edd9','test@qq.com','',1,'','Ai')";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(CREATE_TABLE);
        db.execSQL(CREATE_TABLE_USER);
        db.execSQL(TABLE_NAME_FEED);
        db.execSQL(firstSql);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public boolean login(UserWithChat user) {
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME_USER, null, "(name = ? or email = ?) and password = ?", new String[]{user.getName(), user.getName(), user.getPassword()}, null, null, null);

        boolean userExists = cursor.moveToFirst();
        cursor.close();
        return userExists;
    }


    public boolean insertFeedback(String feedbackType, String feedbackContent) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("feedbackType", feedbackType);
        contentValues.put("feedbackContent", feedbackContent);
        return db.insert("feedback", null, contentValues) > 0;
    }

}
